﻿cms.Lists = function () {

    function _init(pane, showPopupFunction) {
        // Get all icons
        var imgs = document.getElementById(pane).getElementsByTagName("img");
        for (var i = 0; i < imgs.length; i++) {
            if (imgs[i].parentNode.tagName == "SPAN") {
                iLib.Dom.AddListener(imgs[i], "click", function (e) {
                    e = e || window.event;
                    var target = e.target || e.srcElement;
                    iLib.Dom.PreventBubble(e);
                    showPopupFunction(target, target.parentNode.id, target.nextSibling.innerHTML, e);
                });
            }
        }
    }

    function _showPaneOverlay(id) {
        _registerOverlay(id);
        var overlay = document.getElementById(id);
        overlay.style.top = "0px";
        overlay.style.left = "0px";
        overlay.style.width = overlay.parentNode.offsetWidth + "px";
        overlay.style.height = overlay.parentNode.offsetHeight + "px";
        var progress = document.getElementById(id + "Progress");
        var top = parseInt((overlay.offsetHeight - progress.offsetHeight) / 2, 0);
        top = top < 0 ? 0 : top;
        top = top > 30 ? 30 : top;
        progress.style.top = top + "px";
        progress.style.left = parseInt((overlay.offsetWidth - progress.offsetWidth) / 2, 0) + "px";
    }

    function _hidePaneOverlays() {
        for (var i = 0; i < _overlays.length; i++) {
            document.getElementById(_overlays[i]).style.left = -10000 + "px";
            document.getElementById(_overlays[i] + "Progress").style.left = -10000 + "px";
        }
    }

    function _registerPopup(id) {
        for (var i = 0; i < _popups.length; i++) {
            if (_popups[i] == id) {
                return;
            }
        }
        _popups[_popups.length] = id;
    }

    function _registerOverlay(id) {
        for (var i = 0; i < _overlays.length; i++) {
            if (_overlays[i] == id) {
                return;
            }
        }
        _overlays[_overlays.length] = id;
    }

    function _showPopup(popup, trigger, alignment, offsetLeft, offsetTop, toggle, e) {
        _registerPopup(popup.id);
        var alreadyVisible = popup.offsetLeft > -10000;
        _hidePopups(e);
        if (toggle && alreadyVisible && _currentTrigger == trigger) // Toggle popup if clicking multiple times on the same icon
            return;
        var mainBox = document.getElementById("mainBox");
        var top = iLib.Pos.FindY(trigger) + trigger.offsetHeight + offsetTop;
        if (mainBox) {
            top -= iLib.Pos.FindY(mainBox);
        }
        popup.style.top = top + "px";
        var left;
        if (alignment == "left")
            left = iLib.Pos.FindX(trigger) + offsetLeft;
        else {
            var diff = popup.offsetWidth - trigger.offsetWidth;
            left = iLib.Pos.FindX(trigger) + offsetLeft - diff;
        }
        if (mainBox) {
            left -= iLib.Pos.FindX(mainBox);
        }
        popup.style.left = left + "px";
        _currentTrigger = trigger;
        if (_hidePopupsAdded == true)
            return;
        _hidePopupsEvent = function () {
            _hidePopups(e);
        }
        iLib.Dom.AddListener(document, "click", _hidePopupsEvent);
        _hidePopupsAdded = true;
        //alert(popup.offsetLeft + " " + popup.offsetTop);
    }

    function _hidePopups(e) {
        var popup;
        for (var i = 0; i < _popups.length; i++) {
            popup = document.getElementById(_popups[i]);
            popup.style.left = -10000 + "px";
        }
        if (_hidePopupsAdded)
            iLib.Dom.RemoveListener(document, "click", _hidePopupsEvent);
        _hidePopupsAdded = false;
    }

    function _createListPopupMenu(div) {
        // First check that the popup has not already been rendered
        var firstNode = div.childNodes[0];
        while (firstNode.nodeType != 1)
            firstNode = firstNode.nextSibling;
        if (firstNode.tagName.toLowerCase() == "div")
            return;
        // Create the container for the links
        var popupMenuContent = document.createElement("div");
        popupMenuContent.className = "popupMenuContent";
        // Move all links to the container, which in turn will render the parameter div empty
        while (div.childNodes.length > 0) {
            popupMenuContent.appendChild(div.firstChild);
        }
        // Create the top of the menu
        var popupMenuTop = document.createElement("div");
        popupMenuTop.className = "popupMenuTop";
        var popupMenuTopBg = document.createElement("div");
        popupMenuTopBg.className = "popupMenuTopBg";
        popupMenuTopBg.innerHTML = "&nbsp;";
        var popupMenuTopLeftCorner = document.createElement("div");
        popupMenuTopLeftCorner.className = "popupMenuTopLeftCorner";
        popupMenuTopLeftCorner.innerHTML = "&nbsp;";
        var popupMenuTopRightCorner = document.createElement("div");
        popupMenuTopRightCorner.className = "popupMenuTopRightCorner";
        popupMenuTopRightCorner.innerHTML = "&nbsp;";
        popupMenuTop.appendChild(popupMenuTopBg);
        popupMenuTop.appendChild(popupMenuTopLeftCorner);
        popupMenuTop.appendChild(popupMenuTopRightCorner);
        // Put the top of the menu first in the parameter div
        div.appendChild(popupMenuTop);
        // Create the menu part of the popup
        var popupMenu = document.createElement("div");
        popupMenu.className = "popupMenu";
        var popupMenuLeftBg = document.createElement("div");
        popupMenuLeftBg.className = "popupMenuLeftBg";
        popupMenuLeftBg.innerHTML = "&nbsp;";
        var popupMenuRightBg = document.createElement("div");
        popupMenuRightBg.className = "popupMenuRightBg";
        popupMenuRightBg.innerHTML = "&nbsp;";
        popupMenu.appendChild(popupMenuLeftBg);
        popupMenu.appendChild(popupMenuRightBg);
        popupMenu.appendChild(popupMenuContent);
        div.appendChild(popupMenu);
        // Finally, fix the bottom
        var popupMenuBottom = document.createElement("div");
        popupMenuBottom.className = "popupMenuBottom";
        var popupMenuBottomBg = document.createElement("div");
        popupMenuBottomBg.className = "popupMenuBottomBg";
        popupMenuBottomBg.innerHTML = "&nbsp;";
        var popupMenuBottomLeftCorner = document.createElement("div");
        popupMenuBottomLeftCorner.className = "popupMenuBottomLeftCorner";
        popupMenuBottomLeftCorner.innerHTML = "&nbsp;";
        var popupMenuBottomRightCorner = document.createElement("div");
        popupMenuBottomRightCorner.className = "popupMenuBottomRightCorner";
        popupMenuBottomRightCorner.innerHTML = "&nbsp;";
        popupMenuBottom.appendChild(popupMenuBottomBg);
        popupMenuBottom.appendChild(popupMenuBottomLeftCorner);
        popupMenuBottom.appendChild(popupMenuBottomRightCorner);
        div.appendChild(popupMenuBottom);
    }

    var _popups = [];
    var _overlays = [];
    var _currentName;
    var _hidePopupsAdded = false;
    var _hidePopupsEvent;
    var _currentTrigger;

    return {
        Init: _init,
        ShowPaneOverlay: _showPaneOverlay,
        HidePaneOverlays: _hidePaneOverlays,
        CurrentName: _currentName,
        ShowPopup: _showPopup,
        HidePopups: _hidePopups,
        CreateListPopupMenu: _createListPopupMenu
    }

} ();

